package com.guocheng.modules.meet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.base.BaseController;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.dictionarie.model.Dictionarie;
import com.guocheng.modules.dictionarie.utils.Dictionaries;
import com.guocheng.modules.emp.model.po.Emp;
import com.guocheng.modules.emp.service.EmpService;
import com.guocheng.modules.meet.model.po.Meet;
import com.guocheng.modules.meet.model.dto.MeetDTO;
import com.guocheng.modules.meet.service.MeetService;
import com.guocheng.modules.meet.utils.MeetUtils;
import com.guocheng.modules.room.model.po.Room;
import com.guocheng.modules.room.serive.RoomService;
import com.guocheng.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.DateUtils;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/meet")
public class MeetController extends BaseController {

    @Autowired
    private MeetService meetService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private EmpService empService;

    @GetMapping("/index")
    public String index() {
        return "meet/list";
    }

    /**
     * 查询会议信息列表
     *
     * @param meetDTO
     * @param
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult viewsEmp(MeetDTO meetDTO) {
        //会议名称
        if (meetDTO.getName() != null && meetDTO.getName().length()>0)
            meetDTO.setName("like_" + meetDTO.getName());
        //主持人
        if (meetDTO.getEmpName() != null && meetDTO.getEmpName().length()>0)
            meetDTO.setEmpName("like_" + meetDTO.getEmpName());
        //部门
        if(meetDTO.getDeptName()!=null && meetDTO.getDeptName().length()>0){
            meetDTO.setDeptName("like_" + meetDTO.getDeptName());
        }
        QueryWrapper<Meet> queryWrapper = new QueryWrapper<>();
        //开始时间
        if (meetDTO.getStartTime() != null && meetDTO.getStartTime().length()>0) {
            Date startTime = DateUtil.strToDateT(meetDTO.getStartTime());
            queryWrapper.ge("start_time", startTime);
        }
        //结束时间
        if (meetDTO.getEndTime() != null && meetDTO.getEndTime().length()>0) {
            Date endTime = DateUtil.strToDateT(meetDTO.getEndTime());
            queryWrapper.le("start_time", endTime);
        }
        IPage<Meet> meetIPage = meetService.selectPage(meetDTO,queryWrapper);
        return AjaxResult.ok().data(meetIPage.getRecords()).total(meetIPage.getTotal());
    }

    /**
     * 打开信息展示页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/show")
    public String createUser(@RequestParam(value = "id", required = false) Integer id, Model model) {
        if (id != null) {
            Meet meet = meetService.selectById(id);
            model.addAttribute("meet", meet);
            //参与人员回显
            String empsId = meet.getEmpIds();
            model.addAttribute("empsId", empsId==null?null:empsId.split(","));
        }
        List<Dictionarie> meetType = Dictionaries.getDictionaries("meetType");
        model.addAttribute("types", meetType);

        //选择会议室
        List<Room> rooms = roomService.selectList();
        model.addAttribute("rooms", rooms);

        return "meet/show";
    }

    /**
     * 增加一个meet
     *
     * @param meet
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult createMeet(Meet meet
            , @RequestParam(value = "empIds", required = false) List<Integer> empsId        //参会人员id
            , @RequestParam(value = "deptIds", required = false) List<Integer> deptsId     //参会部门id
    ) {

        //检查时间
        meet = MeetUtils.init(meet);

        if(meet==null) return AjaxResult.error("请检查时间区间是否正确！");

        //查询参会人员
        if (!CollectionUtils.isEmpty(empsId) || !CollectionUtils.isEmpty(deptsId)) {
            List<Emp> emps = empService.selectByDeptsEmps(deptsId, empsId);
            meet.setEmps(emps);
        }

        //创建时间
        meet.setCreateDate(new Date());

        //创建人
        meet.setCreateName(user.getUsername());

        //添加
        boolean insert = meetService.insert(meet);

        if (insert) {
            return AjaxResult.ok("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改一个会议
     *
     * @param meet
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult editMeet(Meet meet
            , @RequestParam(value = "empIds", required = false) List<Integer> empsId        //参会人员id
            , @RequestParam(value = "deptIds", required = false) List<Integer> deptsId     //参会部门id
    ) {
        if (meet.getId() == null) return AjaxResult.error("没有id");

        //检查时间
        meet = MeetUtils.init(meet);

        if(meet==null) return AjaxResult.error("请检查时间区间是否正确！");

        //查询参会人员
        if (!CollectionUtils.isEmpty(empsId) || !CollectionUtils.isEmpty(deptsId)) {
            List<Emp> emps = empService.selectByDeptsEmps(deptsId, empsId);
            meet.setEmps(emps);
        }

        //修改
        boolean update = meetService.updateById(meet);
        if (update) {
            return AjaxResult.ok("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 删除会议
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("ids") List<Integer> ids) {
        int flag = meetService.deleteByIds(ids);
        if (flag > 0) {
            return AjaxResult.ok("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
}
